package com.giaothong.ui;

import java.util.ArrayList;

import org.json.JSONObject;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.giaothong.R;
import com.giaothong.controller.GoogleApiController;
import com.giaothong.customview.adapter.ListLocationAdapter;
import com.giaothong.db.table.DtbLikeLocation;
import com.giaothong.http.core.HttpAsyncCallback;
import com.giaothong.http.core.HttpConnectionId;
import com.giaothong.http.core.HttpResult;
import com.giaothong.model.ModelGetLocationInfoGoogleApiRequest;
import com.giaothong.model.entity.LocationSearchEntity;
import com.giaothong.ui.base.BaseActivity;
import com.giaothong.utils.DialogUtil;
import com.giaothong.utils.SessionData;
import com.giaothong.utils.ToastUtils;

public class SearchLocationActivity extends BaseActivity implements HttpAsyncCallback<JSONObject> {

	public static final String EXTRA_ACTION = "location_extra_action";

	private ListView lvSearchLocation;
	private EditText edtSearch;
	private ListLocationAdapter adapter;
	private ArrayList<LocationSearchEntity> arrayList = new ArrayList<LocationSearchEntity>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_search_location);
		init();
		setAction();
	}

	/**
	 * init View Id
	 */
	private void init() {
		((Button) findViewById(R.id.btnNext)).setVisibility(View.INVISIBLE);
		((TextView) findViewById(R.id.txtTitleBar)).setText(R.string.search);
		edtSearch = (EditText) findViewById(R.id.edtSearch);
		lvSearchLocation = (ListView) findViewById(R.id.lvSearchLocation);
		adapter = new ListLocationAdapter(this, android.R.layout.simple_list_item_1, arrayList);
		lvSearchLocation.setAdapter(adapter);
	}

	/**
	 * set Action view
	 */
	private void setAction() {
		// item onclick
		lvSearchLocation.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// show dialog action
				showDialogChooseLocationFromNavigation(arg2);
			}
		});

		edtSearch.addTextChangedListener(new TextWatcher() {

			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if (!edtSearch.getText().toString().equals("")) {
					// GoogleApiController.searchLocationFromText(SearchLocationActivity.this,
					// SearchLocationActivity.this,
					// edtSearch.getText().toString(), "", false);
				}
			}

			public void beforeTextChanged(CharSequence s, int start, int count, int after) {

			}

			public void afterTextChanged(Editable s) {

			}
		});

	}

	/**
	 * search click
	 * 
	 * @param view
	 */
	public void onClickSearch(View view) {
		if (!edtSearch.getText().toString().equals("")) {
			GoogleApiController.searchLocationFromText(SearchLocationActivity.this, SearchLocationActivity.this, edtSearch.getText().toString(), "", true);
		}
	}

	/**
	 * show dialog when click item
	 */
	public void showDialogChooseLocationFromNavigation(final int index) {
		int select[] = { R.string.them_vao_danh_sach_yeu_thich, R.string.dan_duong_den_day, R.string.show_in_map };

		DialogUtil.showListDialog(this, R.string.select, select, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				// neu chon thong bao
				switch (which) {
				// them vao danh sach yeu thich
				case 0: {
					DtbLikeLocation.insertToDatabase(SessionData.getDb(), arrayList.get(index));
					break;
				}
				// navigation select
				case 1: {
					Intent intent = new Intent();
					SessionData.locationSearchEntity = arrayList.get(index);
					intent.putExtra(EXTRA_ACTION, R.string.dan_duong_den_day);
					setResult(RESULT_OK, intent);
					finish();
					break;
				}
				// show map select
				case 2: {
					Intent intent = new Intent();
					SessionData.locationSearchEntity = arrayList.get(index);
					intent.putExtra(EXTRA_ACTION, R.string.show_in_map);
					setResult(RESULT_OK, intent);
					finish();
					break;
				}
				default:
					break;
				}

			}
		});
	}

	/**
	 * ======================================================== process response
	 * from server =========================================================
	 */
	public void onTaskComplete(HttpConnectionId requestCode, HttpResult resultCode, JSONObject result) {
		DialogUtil.showErrorDialogIfRequestFailed(this, resultCode);
		if (resultCode == HttpResult.SUCCESS) {
			switch (requestCode) {
			case SEARCH_LOCATION_FROM_TEXT: {
				ModelGetLocationInfoGoogleApiRequest modelGetLocationInfoGoogleApiRequest = new ModelGetLocationInfoGoogleApiRequest(result);
				if (modelGetLocationInfoGoogleApiRequest.getStatus().equals("OK")) {
					arrayList.clear();
					arrayList.addAll(modelGetLocationInfoGoogleApiRequest.getLocationStatus());
					adapter.notifyDataSetChanged();
				} else {
					ToastUtils.showToast(SearchLocationActivity.this, R.string.error);
				}
				break;
			}
			// default
			default: {
				// do nothing
				break;
			}
			} // end switch
		}
	}

	/**
	 * call this activity
	 * 
	 * @param context
	 */
	public static void show(Context context) {
		Intent intent = new Intent(context, SearchLocationActivity.class);
		context.startActivity(intent);
	}
}
